//
//  WTVideoMainViewController.m
//  OneDay
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTVideoMainViewController.h"
#import "WTVideoCell.h"
#import "WTRecommendListModel.h"
#import "WTRequestManager.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

@interface WTVideoMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

// 存放解析完数据的数组
@property (nonatomic, strong) NSMutableArray * listArray;

// 网络请求起始位置
@property (nonatomic, assign) NSInteger requestStart;

// 等待数据加载完成
@property (nonatomic, strong) MBProgressHUD * hud;

@end

@implementation WTVideoMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _requestStart = 0;
    _listArray = [NSMutableArray array];
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 - 64 - 30 * kRelativelength_Y) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[WTVideoCell class] forCellReuseIdentifier:@"WTVideoCell"];
    
// 明杰刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 0;
        [self requestData];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _requestStart++;
        [self requestData];
    }];
    
    [self setupHud];
    // Do any additional setup after loading the view.
}

#pragma mark -- 等待数据加载完成
- (void)setupHud
{
    _hud = [[MBProgressHUD alloc]initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.labelText = @"正在努力加载...";
    [self.view addSubview:_hud];
    [_hud show:YES];
}

#pragma mark -- 网络请求
- (void)requestData
{
    NSString * url = [NSString stringWithFormat:@"http://api.wap.miercn.com/api/2.0.3/newlist.php?list=8&page=%ld&plat=android&proct=mierapp&versioncode=20150807&apiCode=4",(long)_requestStart];
    
  
    [WTRequestManager requestWithUrlString:url parDic:nil method:GET success:^(NSData *data) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * newsListArray = dic[@"newsList"];
        
        if (_requestStart == 0) {
            [_listArray removeAllObjects];
        }
        for (NSDictionary * oneDic in newsListArray) {
            WTRecommendListModel * model = [[WTRecommendListModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
        }
        
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_hud hide:YES];
    } error:^(NSError *error) {
        
    }];

}

#pragma mark -- tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTVideoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WTVideoCell" forIndexPath:indexPath];
    cell.model = _listArray[indexPath.row];
    return cell;
}
// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    return 120 * kRelativelength_Y;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTRecommendListModel * model = _listArray[indexPath.row];
    self.block(model.ID,model.commentNum);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
